<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="form.newPassword" placeholder="请输入密码" show-password auto-complete="new-password" />
        </el-form-item>
        <el-form-item label="再次输入" prop="againPassword">
          <el-input type="password" v-model="form.againPassword" placeholder="请再次输入密码" show-password />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { editPassword } from "@/api/enterprise/user";
export default {
  props: {
    title: {
      default: "修改密码",
    },
    value: {
      default: false,
    },
    userId: {
      default: undefined,
    },
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        newPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        againPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('请再次输入密码'));
              }
              if (value !== this.form.newPassword) {
                return callback(new Error('密码不一致，请重新输入'));
              }
              callback();
            }, trigger: 'blur'
          }
        ],
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
    };
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      // this.dateRange = [];
      this.form = {
        newPassword: undefined,
        againPassword: undefined,
      }
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.newPassword !== this.form.againPassword) {
            this.$message.error('密码不一致，请重新输入')
            return
          }
          let data = {
            userId: this.userId,
            newPassword: this.form.newPassword,
            againPassword: this.form.againPassword
          }
          editPassword(data).then(response => {
            if (response.code == 200) {
              this.$emit("success");
              this.$message.success(response.msg);
              this.visible = false;
            } else {
              this.$message.error(response.msg);
            }
          })
        }
      })

    },
    cancel() {
      this.reset();
      this.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";
</style>
